<template>
	<view class="my-settle-container">
		<!-- 全选区域 -->
		<label class="radio" @click="changeAllState">
			<radio color="#c00000" :checked="isFullCheck" /><text>全选</text>
		</label>
		<!-- 合计区域 -->
		<view class="amount-box">
			合计<text class="amount">￥{{checkedGoodsAmount}}</text>
		</view>
		<!-- 结算按钮 -->
		<view class="btn-settle" @click="settlement">
			结算({{checkedCount}})
		</view>
	</view>
</template>

<script>
	import {mapGetters,mapMutations,mapState } from 'vuex'
	export default {
		data() {
			return {
				seconds:3,
				timer:null
			};
		},
		computed:{
			//结算货物数量    购物车总量   勾选货物价格
			...mapGetters('m_cart',['checkedCount','total','checkedGoodsAmount']),
			...mapGetters('m_user',['addressStr']),
			...mapState('m_user',['token']),
			//是否全选
			isFullCheck(){
				return this.checkedCount == this.total
			}
		},
		methods:{
			...mapMutations('m_user', ['updateRedirectInfo']),
			...mapMutations('m_cart',['updateAllGoodsState']),
			changeAllState(){
				this.updateAllGoodsState(!this.isFullCheck)
			},
			settlement(){
				//判断是否有货物
				if(!this.checkedCount)
					return uni.$showMsg('请选择要结算的商品！')
				//判断是否登录,如果没有登录，则调用 delayNavigate() 进行倒计时的导航跳转
				if(!this.token)
					// return uni.$showMsg('请先登录！')
					return this.delayNavigate()
				//判断是否选择了收货地址
				if(!this.addressStr){
					return uni.$showMsg('请选择收货地址！')
				}	
			},
			showTips(n){
				uni.showToast({
					title: '请登录后再结算！' + n + ' 秒后自动跳转到登录页',
					icon: 'none',
					duration: 1500,
					 // 为页面添加透明遮罩，防止点击穿透
					mask:true,
				});
			},
			delayNavigate(){
				this.seconds = 3
				this.showTips(this.seconds);
				this.timer = setInterval(()=>{
					this.seconds--
					if(this.seconds <= 0){
						//清除定时器
						clearInterval(this.timer)
						//跳转到my页面
						uni.switchTab({
							url:'/pages/my/my',
							success:()=>{
								// 调用 vuex 的 updateRedirectInfo 方法，把跳转信息存储到 Store 中
								this.updateRedirectInfo({
									openType:'switchTab',
									from:'/pages/cart/cart'
								})
							}
						})
						//终止后续代码的运行（当秒数为 0 时，不再展示 toast 提示消息）
						return
					}
					this.showTips(this.seconds)
				},1000)
			}
		}
	}
</script>

<style lang="scss">
.my-settle-container{
	position: fixed;
	bottom: 0;
	left: 0;
	height: 50px;
	width: 100%;
	background-color: white;
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 14px;
	
	.radio{
		padding-left: 5px;
		display: flex;
		align-items: center;
	}
	.amount{
		color: #c00000;
	}
	.btn-settle {
    height: 50px;
    min-width: 100px;
    background-color: #c00000;
    color: white;
    line-height: 50px;
    text-align: center;
    padding: 0 10px;
  }
}
</style>
